﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CLEXA.SurfaceClient.ViewModel
{
    public class DatabaseReadVM : VMBase
    {
        private static readonly DatabaseIdentityComparer identityComparer = new DatabaseIdentityComparer();
        private readonly int databaseId;
        private readonly int userId;
        private readonly string server;
        private readonly string database;
        private readonly string connectionString;

        public static IEqualityComparer<DatabaseReadVM> IdentityComparer
        {
            get { return DatabaseReadVM.identityComparer; }
        }

        public int DatabaseId
        {
            get { return databaseId; }
        }

        public int UserId
        {
            get { return userId; }
        }

        public string Server
        {
            get { return server; }
        }

        public string Database
        {
            get { return database; }
        }

        public string ConnectionString
        {
            get { return connectionString; }
        }

        public DatabaseReadVM(int databaseId, int userId, string server, string database, string connectionString)
        {
            this.databaseId = databaseId;
            this.userId = userId;
            this.server = server;
            this.database = database;
            this.connectionString = connectionString;
        }

        private sealed class DatabaseIdentityComparer : IEqualityComparer<DatabaseReadVM>
        {
            public bool Equals(DatabaseReadVM x, DatabaseReadVM y)
            {
                if (Object.ReferenceEquals(x, y))
                {
                    return true;
                }
                if (Object.ReferenceEquals(x, null))
                {
                    return false;
                }
                if (Object.ReferenceEquals(y, null))
                {
                    return false;
                }

                return x.database == y.database;
            }

            public int GetHashCode(DatabaseReadVM obj)
            {
                if (Object.ReferenceEquals(obj, null))
                {
                    return 0;
                }

                return obj.database.GetHashCode();
            }
        }
    }
}
